<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>字典类型</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
</head>
<body>
<!--添加功能的form-->
<form class="layui-form" action="" id="insertDicTypeFrom"
      style="display: none;margin-right: 40px;margin-top: 20px">
    <div class="layui-form-item">
        <label class="layui-form-label">类型名称</label>
        <div class="layui-input-block">
            <input type="text" name="typeName" autocomplete="off" placeholder="类型名称"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="remark" autocomplete="off" placeholder="备注"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="insertDicType">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-normal">重置</button>
        </div>
    </div>
</form>

<!--编辑功能的form-->
<form class="layui-form" action="" id="updateDicTypeFrom" lay-filter="updateDicTypeFrom"
      style="display: none;margin-right: 40px;margin-top: 20px">
    <div class="layui-form-item">
        <label class="layui-form-label">类型ID</label>
        <div class="layui-input-block">
            <input type="text" name="typeId" autocomplete="off" placeholder="类型ID"
                   readonly="readonly" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">类型名称</label>
        <div class="layui-input-block">
            <input type="text" name="typeName" autocomplete="off" placeholder="类型名称"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="remark" autocomplete="off" placeholder="备注"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateDicTypeFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-normal">重置</button>
        </div>
    </div>
</form>

<!--顶部搜索框-->
<form class="layui-form" action="" style="margin-top: 10px">
    <div class="layui-inline">
        <label class="layui-form-label">类型ID</label>
        <div class="layui-input-inline">
            <input type="text" name="typeId" autocomplete="off" placeholder="类型ID" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">类型名称</label>
        <div class="layui-input-inline">
            <input type="text" name="typeName" autocomplete="off" placeholder="类型名称" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-inline">
            <input type="text" name="createBy" autocomplete="off" placeholder="创建人" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="searchFilter">搜索</button>
            <button type="reset" class="layui-btn layui-btn-normal" id="myButton">重置</button>
        </div>
    </div>
</form>
<table id="showDicType" lay-filter="dicTypeFilter"></table>
</body>
<!--顶部工具栏-->
<script type="text/html" id="dicToolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addDicType">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
    </div>
</script>
<!--实现操作功能按钮-->
<script type="text/html" id="dicTypeBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>
</script>
<script>
    layui.use(['table', 'layer', 'jquery', 'form'], function () {
        let table = layui.table;
        let layer = layui.layer;
        let $ = layui.jquery;
        let form = layui.form;

        let dicTypeTable = table.render({
            elem: '#showDicType'
            , url: '/dictionaryType/queryAll' //数据接口
            , toolbar: '#dicToolbar' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print']
            , title: 'dicTypeTable'
            , page: true
            , cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'typeId', title: '类型ID', width: 100, sort: true}
                    , {field: 'typeName', title: '类型名称', width: 120}
                    , {field: 'createBy', title: '创建人', width: 100}
                    , {field: 'createTime', title: '创建时间', width: 200}
                    , {field: 'updateBy', title: '修改人', width: 100}
                    , {field: 'updateTime', title: '修改时间', width: 200}
                    , {field: 'remark', title: '备注', width: 200}
                    , {fixed: 'right', align: 'center', title: '操作', toolbar: '#dicTypeBar', width: 120}
                ]
            ]
        });
        //监听行工具事件
        table.on('tool(dicTypeFilter)', function (obj) {
            let data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'edit') {
                form.val('updateDicTypeFrom', {
                    'typeId': data.typeId,
                    'typeName': data.typeName,
                    'remark': data.remark
                });
                layer.open({
                    title: '编辑字典类型',
                    type: 1,
                    area: ['500px', '500px'],
                    content: $("#updateDicTypeFrom")
                });
            }else if (layEvent === 'delete'){
                // alert(data.typeId)
                layer.confirm('真的删除行么', function(index){
                    //向后台发送异步请求
                    $.ajax({
                        url:"/dictionaryType/deleteById",
                        type:"DELETE",
                        data:{dictionaryTypeId:data.typeId},
                        dataType:"json",
                        success:function (resp) {
                            if(resp.code==0){
                                layer.msg(resp.msg);
                                //重新加载数据
                                dicTypeTable.reload({
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                })
                            }
                        },
                        error:function (e) {
                            layer.msg("请求失败");
                        }
                    });
                });
            }
        });
        //form表单实现更新功能
        form.on('submit(updateDicTypeFilter)', function (data) {
            //提交表单数据
            $.ajax({
                url: "/dictionaryType/update",
                type: "PUT",
                dataType: "JSON",
                data: data.field,
                success: function (ret) {
                    if (ret.code == 0) {
                        //关闭对话框
                        layer.closeAll();
                        //刷新表格数据
                        dicTypeTable.reload({
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        layer.msg(ret.msg, {icon: 6});
                    } else {
                        layer.msg(ret.msg, {icon: 5});
                    }

                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //监听头工具栏事件
        table.on('toolbar(dicTypeFilter)', function (obj) {
            let checkStatus = table.checkStatus(obj.config.id);
            let  checkData = checkStatus.data;
            switch (obj.event) {
                case 'addDicType':
                    document.getElementById("insertDicTypeFrom").reset();
                    layer.open({
                        title: '添加字典类型',
                        type: 1,
                        area: ['500px', '450px'],
                        content: $("#insertDicTypeFrom")
                    });
                    break;
                case 'delete':
                    //判断是否选中行
                    if(checkData.length==0){
                        layer.msg('请至少选择一行数据');
                        return;
                    }
                    layer.confirm('真的删除行么', function(index) {
                        //获取所有选中行id
                        let dictionaryTypeIds = "";
                        for (let i = 0; i < checkData.length; i++) {
                            dictionaryTypeIds += checkData[i].typeId + ",";
                        };
                        dictionaryTypeIds = dictionaryTypeIds.substr(0, dictionaryTypeIds.length - 1);
                        layer.msg(dictionaryTypeIds);
                        alert(dictionaryTypeIds)
                        //调用批量删除
                        $.ajax({
                            url: "/dictionaryType/deleteByIds",
                            type: "DELETE",
                            data: {dictionaryTypeIds:dictionaryTypeIds},
                            dataType: "json",
                            success: function (resp) {
                                if (resp.code == 0) {
                                    layer.msg(resp.msg);
                                    //重新加载数据
                                    dicTypeTable.reload();
                                }
                            },
                            error: function (e) {
                                layer.msg("请求失败");
                            }
                        });
                    });
                    break;
                case 'update':

                    if(checkData.length!=1){
                        layer.msg('编辑只能选中一行');
                        return;
                    }
                    form.val("updateDicTypeFrom",checkData[0]);
                    layer.open({
                        type: 1,
                        area:['400px', '350px'],
                        content: $('#updateDicTypeFrom') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
            };
        });

        //form表单实现添加功能
        form.on('submit(insertDicType)', function (data) {
            //提交表单数据
            $.ajax({
                url: "/dictionaryType/insert",
                type: "POST",
                dataType: "JSON",
                data: data.field,
                success: function (ret) {
                    if (ret.code == 0) {
                        //关闭对话框
                        layer.closeAll();
                        //刷新表格数据
                        dicTypeTable.reload({
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        layer.msg(ret.msg, {icon: 6});
                    } else {
                        layer.msg(ret.msg, {icon: 5});
                    }

                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //实现顶部搜索框
        form.on('submit(searchFilter)', function (data) {
            //执行重载
            dicTypeTable.reload({
                where: {
                    typeId: data.field.typeId,
                    typeName: data.field.typeName,
                    createBy: data.field.createBy
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //重置条件查询表单，刷新table数据
        $("#myButton").click(function () {
            dicTypeTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    typeId: ""
                    , typeName: ""
                    , createBy: ""
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
    });
</script>
</html>